#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_LENGTH 256

int get_sum_child_string(char *str, char *child_str);

int main(void) {
	char string[MAX_LENGTH];
	char child_string[MAX_LENGTH];
	printf("请输入两个字符串和字符串——不能包含空格！\n");
	scanf("%s%s", string, child_string);
	printf("子字符串出现的次数：%d\n", get_sum_child_string(string, child_string));

	return 0;
}

int get_sum_child_string(char *str, char *child_str) {
	int sum = 0;
	int i = 0;
	int str_len = strlen(str);
	int child_str_len = strlen(child_str);
	char *p_tmp = NULL;

	//动态分配内存空间
	p_tmp = (char *)malloc(sizeof(MAX_LENGTH));
	if (p_tmp == NULL) {
		perror("error, malloc is failed !\n");
		return -1;
	}
	p_tmp = str;

	//判定数据是否合理
	if (str == NULL || child_str == NULL || str_len < child_str_len) {
		return 0;
	}

	//循环匹配
	while (strlen(p_tmp) >= child_str_len) {
		//单个字母匹配
		for (i = 0; i < child_str_len; i++) {
			if (p_tmp[i] != child_str[i])
				break;
		}
		//匹配到字符串
		if (i == child_str_len) {
			sum++;
			p_tmp += child_str_len;
		} else p_tmp++;
	}

	//释放临时变量空间
	if (p_tmp != NULL) {
		free(p_tmp);
	}

	return sum;
}
